import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import viteCompression from "vite-plugin-compression";
import VueSetupExtend from "unplugin-vue-define-options/vite";
// import { svgBuilder } from "./src/components/icon/svg/index";

// import legacyPlugin from '@vitejs/plugin-legacy'
/**
 * AutoImport Components ElementPlusResolver都是将element-ui按需导入的依赖
 */
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import ElementPlus from "unplugin-element-plus/vite";

// https://vitejs.dev/config/
export default defineConfig({
  build: {
    rollupOptions: {
      output: {
        chunkFileNames: "static/js/[name]-[hash].js",
        entryFileNames: "static/js/[name]-[hash].js",
        assetFileNames: "static/[ext]/[name]-[hash].[ext]",
        manualChunks(id) {
          if (id.includes("node_modules")) {
            return id
              .toString()
              .split("node_modules/")[1]
              .split("/")[0]
              .toString();
          }
        },
      },
    },
    minify: "terser",
    terserOptions: {
      //清除console和debugger
      compress: {
        drop_console: true,
        drop_debugger: true,
      },
    },
    chunkSizeWarningLimit: 1000, //超静态资源过大，默认500kb会预警 设置1000kb
  },
  define: {
    "process.env": {}, //process.env 已经被遗弃
  },
  plugins: [
    vue(),
    // svgBuilder("./src/assets/icons/"),
    VueSetupExtend(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    ElementPlus({
      // options
    }),
    // legacyPlugin({
    //   targets: ["chrome 52"], // 需要兼容的目标列表，可以设置多个
    //   additionalLegacyPolyfills: ["regenerator-runtime/runtime"], // 面向IE11时需要此插件
    // }),
    viteCompression({
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: "gzip",
      ext: ".gz",
    }),
  ],
  resolve: {
    alias: {
      "@": resolve(__dirname, "./src"),
      "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.prod.js",
    },
  },
  server: {
    open: false,
    https: false,
    hmr: true, //热更新
    proxy: {
      "/prod-api": {
        // target: "http://192.168.0.106:8080",
        // target: "http://192.168.3.12:28008",//本地
        // target: "http://47.103.39.74:28009",//打包
        // target: "http://120.77.31.204:28009", //打包
        // target: "http://47.103.39.74:28008",
        target: "http://120.77.31.204:28008",
        // target: "http://106.14.239.150:9090",
        // target: "http://106.14.239.150:8090",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/prod-api/, ""),
      },
    },
  },
});
